﻿using System;
using System.Text;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections.Specialized;

using Microsoft.Maps.MapControl;
using Microsoft.Maps.MapControl.Navigation;

using SBPweb.Forge.Controls.Silverlight.Utilities;

using SBPweb.Accelerators.StreetView.Client.BingMapsProvider.Models;
using SBPweb.Accelerators.StreetView.Client.BingMapsProvider.Controllers;

namespace SBPweb.Accelerators.StreetView.Client.BingMapsProvider.Controls
{

    public partial class MapViewer : UserControlBase
    {

        #region Private fields

        private MapPolyline routePoly = new MapPolyline();

        #endregion

        #region Constructor logic

        public MapViewer()
        {
            InitializeComponent();
            InitializeHandlers();
        }

        #endregion

        #region Property accessors

        public IMapViewer Model
        {
            get
            {
                return MapViewController.Model;
            }
        }

        #endregion

        #region Events

        #endregion

        #region Methods

        #region Initialization

        private void InitializeHandlers()
        {
            //StreetsideMode.AddModeToNavigationBar(mapViewer);
            string qpNav = "visible";
            string qpUrl = string.Empty;
            if (HtmlPage.Document.QueryString.ContainsKey("navigation"))
            {
                qpNav = HtmlPage.Document.QueryString["navigation"];
            }
            if (HtmlPage.Document.QueryString.ContainsKey("url"))
            {
                qpUrl = HtmlPage.Document.QueryString["url"];
            }
            Visibility visNav = (Visibility)Enum.Parse(typeof(Visibility), qpNav, true);

            mapViewer.ViewChangeStart += new EventHandler<MapEventArgs>(mapViewer_ViewChangeStart);
            mapViewer.ViewChangeEnd += new EventHandler<MapEventArgs>(mapViewer_ViewChangeEnd);
            mapViewer.MouseClick += new EventHandler<MapMouseEventArgs>(mapViewer_MouseClick);

            mapViewer.NavigationVisibility = visNav;
            mapViewer.MapForeground.TemplateApplied += new EventHandler(MapForeground_TemplateApplied);


            //TransformGroup tg = new TransformGroup();
            //MatrixTransform mt = mapViewer.MapForeground.NavigationBar.RenderTransform as MatrixTransform;
            //ScaleTransform st = new ScaleTransform();
            //st.ScaleX = 0.5;
            //st.ScaleY = 0.5;
            //st.CenterX = 0.5;
            //st.CenterY = 0.5;
            //tg.Children.Add(mt);
            //tg.Children.Add(st);
            //mapViewer.MapForeground.NavigationBar.RenderTransform = tg;

            Model.PropertyChanged += new PropertyChangedEventHandler(Model_PropertyChanged);
            //Model.PolygonCreated += new EventHandler<PolygonEventArgs>(Model_PolygonCreated);
            //Model.RouteCreated += new EventHandler<PolygonEventArgs>(Model_RouteCreated);
        }

        #endregion

        #region Event handlers

        private void MapForeground_TemplateApplied(object sender, EventArgs e)
        {
            mapViewer.MapForeground.NavigationBar.TemplateApplied += new EventHandler(NavigationBar_TemplateApplied);
        }

        private void NavigationBar_TemplateApplied(object sender, EventArgs e)
        {
            NavigationBar navControl = mapViewer.MapForeground.NavigationBar;

            FullScreenModeToggleCommandButton fsmtcb = new FullScreenModeToggleCommandButton();
            CommandToggleButton toggleScaleButton = new CommandToggleButton(fsmtcb, "Full Screen", "Toggle Full Screen Mode");
            toggleScaleButton.Click += new RoutedEventHandler(fsmtcb.ClickHandler);
            navControl.HorizontalPanel.Children.Add(toggleScaleButton);
        }

        private void mapViewer_ViewChangeStart(object sender, MapEventArgs e)
        {
            Model.RaiseViewChanging();
        }

        private void mapViewer_ViewChangeEnd(object sender, MapEventArgs e)
        {
            Model.RaiseViewChanged();
        }

        private void mapViewer_MouseClick(object sender, MapMouseEventArgs e)
        {
            //if (poly == null)
            //{
            //    return;
            //}
            Location location = mapViewer.ViewportPointToLocation(e.ViewportPoint);
            //poly.Add(location);
        }

        private void tbkStartDraw_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (routePoly != null)
            {
                mapViewer.Children.Remove(routePoly);
            }

            //poly = new ITSH.Carbooking.Client.Logic.Services.Location.Polygon();
            //Model.CreateRoute("Route A", poly);
            //poly.CollectionChanged += new NotifyCollectionChangedEventHandler(poly_CollectionChanged);
        }

        private void poly_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            //switch (e.Action)
            //{
            //    default:
            //        break;
            //    case NotifyCollectionChangedAction.Add:
            //        routePoly.Locations.Add((e.NewItems[0] as ITSH.Carbooking.Client.Logic.Services.Location.Location));
            //        break;
            //    case NotifyCollectionChangedAction.Remove:
            //        routePoly.Locations.Remove((e.OldItems[0] as ITSH.Carbooking.Client.Logic.Services.Location.Location));
            //        break;
            //}
        }

        private void tbkEndDraw_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            //foreach (ITSH.Carbooking.Client.Logic.Services.Location.Location l in poly)
            //{
            //    sb.AppendFormat("\tnew Location({0}, {1});\n", l.Latitude, l.Longitude);
            //}
        }

        private void Model_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "ViewMode")
            {
                mapViewer.Mode = Model.ViewMode;
            }
        }

        //private void Model_PolygonCreated(object sender, PolygonEventArgs e)
        //{
        //    MapPolyline polyline = CreateMapPolyline(
        //        e.Name, 
        //        e.Polygon,
        //        Color.FromArgb(255, 0, 90, 170),
        //        Color.FromArgb(255, 117, 194, 60),
        //        Colors.Black,
        //        Colors.White);
        //}

        //private void Model_RouteCreated(object sender, PolygonEventArgs e)
        //{
        //    MapPolyline polyline = CreateMapPolyline(
        //        e.Name,
        //        e.Polygon,
        //        Colors.Transparent,
        //        Colors.Red,
        //        Colors.Black,
        //        Colors.White);
        //    routePoly = polyline;
        //}

        //private MapPolyline CreateMapPolyline(string caption, ITSH.Carbooking.Client.Logic.Services.Location.Polygon polygon, Color fill, Color stroke, Color captionForeground, Color captionShadow)
        //{
        //    MapPolyline polyline = new MapPolyline();
        //    if (fill != Colors.Transparent)
        //    {
        //        polyline.Fill = new SolidColorBrush(fill);
        //    }
        //    polyline.Stroke = new SolidColorBrush(stroke);
        //    polyline.StrokeThickness = 5;
        //    polyline.Opacity = 0.7;
        //    polyline.Locations = new LocationCollection();
        //    foreach (Location loc in polygon)
        //    {
        //        polyline.Locations.Add(loc);
        //    }
        //    mapViewer.Children.Add(polyline);

        //    if (polygon.Count == 0)
        //    {
        //        return polyline;
        //    }
        //    MapLayer layer = new MapLayer();
        //    Location centerPoint = polygon[0];
        //    DropShadowEffect dse = new DropShadowEffect();
        //    dse.BlurRadius = 5;
        //    dse.Direction = 0;
        //    dse.Color = captionShadow;
        //    dse.ShadowDepth = 3;
        //    TextBlock tbk = new TextBlock();
        //    tbk.Foreground = new SolidColorBrush(captionForeground);
        //    tbk.FontWeight = FontWeights.Bold;
        //    tbk.FontSize = 15;
        //    tbk.Effect = dse;
        //    tbk.Text = caption;
        //    layer.AddChild(tbk, centerPoint);

        //    mapViewer.Children.Add(layer);

        //    return polyline;
        //}

        #endregion

        #endregion

    }

}